﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Text;
using System.IO;

namespace BoxDiary
{
    public partial class WriteDiary : UserControl
    {
        public WriteDiary()
        {
            InitializeComponent();
        }

        private void WriteDiary_Load(object sender, EventArgs e)
        {
            //从外部文件加载字体文件  
            PrivateFontCollection font = new PrivateFontCollection();
            font.AddFontFile(Environment.CurrentDirectory + @"\Fonts\思源黑体.ttf");
            //检测字体类型是否可用
            var r = font.Families[0].IsStyleAvailable(FontStyle.Regular);
            var b = font.Families[0].IsStyleAvailable(FontStyle.Bold);
            //定义成新的字体对象
            FontFamily myFontFamily = new FontFamily(font.Families[0].Name, font);
            Font myFont = new Font(myFontFamily, 12F, FontStyle.Regular);
            //将字体显示到控件  
            label1.Font = myFont;

            //将字体加载到选择缓冲区
            string path = $@"{Application.StartupPath}\Fonts";
            List<string> allfile = new List<string>();
            foreach (string filename in Directory.GetFiles(path))
            {
                FileInfo file = new FileInfo(filename);
                allfile.Add(file.Name);
            }

            foreach (string a in allfile)
            {
                toolStripComboBox1.Items.Add(a.Split('.')[0]);
            }

            textBox2.UseSystemPasswordChar = true;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            string nowDt = DateTime.Now.ToString();
            string dW = $"时间：{nowDt}\n密码：{textBox2.Text}\n";
            dW = AES.EncryptNew.AESEncrypt(
                dW, "METHODBOXSTUDIO"
                );
            richTextBox1.Text = AES.EncryptNew.AESEncrypt(
                richTextBox1.Text,textBox2.Text
                );
            string bak = richTextBox1.Text;
            richTextBox1.Text = $"{dW};{bak}";
            if (textBox1.Text == "") textBox1.Text = "Untitled";
            richTextBox1.SaveFile(Application.StartupPath + 
                $"\\Diary\\{textBox1.Text}.rtf", RichTextBoxStreamType.RichText);
            if (Main.SAE)
            {
                File.Encrypt(Application.StartupPath +
                $"\\Diary\\{textBox1.Text}.rtf");
            }
            MessageBox.Show("保存成功！","Successful",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            richTextBox1.Cut();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            richTextBox1.Copy();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            richTextBox1.Hide();
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            richTextBox1.Show();
        }

        private void toolStripComboBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                //从外部文件加载字体文件  
                PrivateFontCollection font = new PrivateFontCollection();
                font.AddFontFile(Environment.CurrentDirectory + $@"\Fonts\{toolStripComboBox1.Text}.ttf");
                //检测字体类型是否可用
                var r = font.Families[0].IsStyleAvailable(FontStyle.Regular);
                var b = font.Families[0].IsStyleAvailable(FontStyle.Bold);
                //定义成新的字体对象
                FontFamily myFontFamily = new FontFamily(font.Families[0].Name, font);
                Font myFont = new Font(myFontFamily, 10.2F, FontStyle.Regular);

                richTextBox1.Font = myFont;

            }
            catch
            {

            }
        }

        public void ToggleBold()
        {
            if (richTextBox1.SelectionFont == null)
                richTextBox1.SelectionFont = richTextBox1.Font;

            FontStyle style = richTextBox1.SelectionFont.Style;

            if (richTextBox1.SelectionFont.Bold)

                style &= ~FontStyle.Bold;//恢复正常
            else
                style |= FontStyle.Bold;

            richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, style);
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
